// JavaScript Document
function trimAll(sString){
	//removes whitespaces from left & right of string
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function charsStrip(string,list){
	//go through string and remove any chars on list
	var i;
	var result = "";
	
	for(i = 0; i<string.length;i++){
		//check current char
		var c = string.charAt(i);
		if(list.indexOf(c) == -1)
			result += c;
	}
	return result
}

function validateField(name,inputField, helpText){
		// Check fields and correct characters
		
		//determine which field
		if(name == "name"){
			
			if(inputField.value.length == 0){
				//field is empty
				
				if(helpText != "final") //sendQuote needs to skip this
					helpText.innerHTML = "Please enter a name.";
				
				return false;
			}
			else{
				//Field is OK
				
				if (helpText != null)
					helpText.innerHTML = "";
				
				return true;
			}
			
		}
		else if(name == "number"){
			if(inputField.value.length != 0){
				//field is non-empty, check digits
				var digits = "0123456789";
				var phoneDelimiters = "()-., ";
				var minDigits = 10;
				var num = inputField.value;
				
				num = trimAll(num);
				num = charsStrip(num,phoneDelimiters);
				if((isInteger(num))&&(num.length >= minDigits)){
					helpText.innerHTML = "";
					return true;
				}
				else{
					helpText.innerHTML = "Invalid Number";
					return false;
				}
				
			}
			else{
				//Field is empty which is OK
				if (helpText != null)
					helpText.innerHTML = "";
				return true;
			}
		}
		else if(name == "email"){
			if(inputField.value.length != 0){
				//check email for '@' symbol followed by '.'
				var i,j;
				var string = inputField.value;
				for(i=0;i<string.length;i++){
					if(string.charAt(i) == '@'){
						for(j=i;j<string.length;j++){
							if(string.charAt(j) == '.'){
								helpText.innerHTML = "";
								return true;
							}
						}
					}
				}
				//if we reach this far, email invalid
				helpText.innerHTML = "Invalid Email";
				return false;
			}
			else{
				//Field is empty which is OK
				if (helpText != null)
					helpText.innerHTML = "";
				return true;
			}
			
		}
		else if(name == "address"){
			//no checks needed
			return true;
		}
		else if(name == "postalcode"){
			var postalDelimiters = "- ";
			var value = inputField.value;
			
			value = trimAll(value);
			value = charsStrip(value,postalDelimiters);
			
			//Canadian Postal Code needs exactly 6 chars
			if(value.length == 6){
				helpText.innerHTML = "";
				return true;
			}
			else{
				helpText.innerHTML = "Invalid Postal Code";
				return false;
			}
		}
		else if(name == "extrainfo"){
			//no checks needed
			return true;
		}
		else{
			//should never reach here!
			alert("Reached end of ValidateField, no match!");
		}
		
}

function handleDisplayToggle(formName,formField,displayButton){
	//handle show/hide for getquote form
	
	if(formName.elements[formField].style.display == ""){
		formName.elements[formField].style.display = "block";
		displayButton.alt ="Hide";
		displayButton.src ="hide.jpg";
	}
	else if(formName.elements[formField].style.display == "block"){
		formName.elements[formField].style.display = "";
		displayButton.alt ="Show";
		displayButton.src ="show.jpg";
	}
	else{
		//should never reach here
		alert("Reached end of handleDisplayToggle, no match!");
	}
}

function sendQuote(form){
	
	var field = "final";
	//do final check on quote form
	if(!validateField("name",form.elements["name"],field)){
		alert("Please enter a name.");
	}
	else if(!validateField("postalcode",form.elements["postalcode"],field)){
		alert("Please enter a postal code.");
	}
	else if((form.elements["email"].value.length==0)&&(form.elements["number"].value.length== 0)){
		alert("Please enter a number or email so that we may reach you with your quote.");
	}
	else if(!validateField("email",form.elements["email"],field)){
		alert("Please enter a valid email address.");
	}
	else if(!validateField("number",form.elements["number"],field)){
		alert("Please enter a valid number.");
	}
	else{
		form.submit();
	}
}